<template>
  <div class="employees-container">
    <!-- 提供了一些默认的内边距 padding -->
    <div class="app-container">
      <PageTools>1
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/import?type=user')">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="isShow=true">新增员工</el-button>
        </template>
      </PageTools>

      <el-card v-loading="loading" style="margin-top: 10px">
        <el-table :data="list" border>
          <el-table-column label="序号" type="index" :index="indexMethod" sortable="" />
          <el-table-column label="姓名" prop="username" sortable="" />
          <el-table-column label="头像" prop="staffPhoto">
            <template #default="{ row }">
              <img v-imgerror="defaultImage" class="staff" :src="row.staffPhoto || defaultImage" alt="" @click="showDialog(row.staffPhoto)">
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="mobile" sortable="" />
          <el-table-column label="工号" prop="workNumber" sortable="" />
          <el-table-column label="聘用形式" :formatter="formatEmployment" prop="formOfEmployment" sortable="" />
          <el-table-column label="部门" prop="departmentName" sortable="" />
          <el-table-column label="入职时间" sortable="">
            <template #default="{row}">
              {{ row.timeOfEntry | formatTime }}
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="handleRoleAssign(row.id)">角色</el-button>
              <el-button type="text" size="small" @click="delEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            :current-page="page"
            :page-sizes="[1, 2, 3, 4,5]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
      <AddEmployee :show-dialog.sync="isShow" @addEmployee="getEmployeeList" />
      <AssignRole :show-role-dialog.sync="showRoleDialog" :user-id="userId" />
      <el-dialog title="二维码" :visible="isDialogShow" @close="closeDialog">
        <el-row type="flex" justify="center">
          <canvas ref="myCanvas" />
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reqDelEmployee, reqGetEmployeeList } from '@/api/employees'
import Employment from '@/constant/employees'
import AddEmployee from './components/AddEmployee'
import AssignRole from './components/assign-role'
import defaultImage from '@/assets/common/bigUserHeader.png'
import QrCode from 'qrcode'
export default {
  name: 'Employees',
  components: {
    AddEmployee,
    AssignRole
  },
  data() {
    return {
      page: 1,
      pagesize: 5,
      list: [],
      total: 0,
      loading: false,
      isShow: false,
      defaultImage: defaultImage,
      isDialogShow: false,
      showRoleDialog: false,
      userId: ''
    }
  },
  computed: {},
  created() {
    this.getEmployeeList()
  },
  methods: {
    async getEmployeeList() {
      this.loading = true
      const { data: { rows, total }} = await reqGetEmployeeList(this.page, this.pagesize)
      // console.log(rows, total)
      this.list = rows
      this.total = total
      this.loading = false
    },
    handleSizeChange(val) {
      // console.log(val)
      this.pagesize = val
      this.page = 1
      this.getEmployeeList()
    },
    handleCurrentChange(val) {
      // console.log(val)
      this.page = val
      this.getEmployeeList()
    },
    indexMethod(index) {
      return index + 1 + this.pagesize * (this.page - 1)
    },
    formatEmployment(row, column, cellValue, index) {
      // console.log(row, column, cellValue, index)
      const obj = Employment.hireType.find(item => item.id === cellValue)
      return obj ? obj.value : '未知'
    },
    delEmployee(id) {
      this.$confirm('此操作不可逆，是否确认删除?', '警告', {
        type: 'warning'
      }).then(async() => {
        const res = await reqDelEmployee(id)
        this.$message.success(res.message)
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        this.getEmployeeList()
      }).catch(() => {
        console.log('cancel')
      })
    },
    showDialog(url) {
      if (!url) return // 有图片才显示弹层
      this.isDialogShow = true
      this.$nextTick(() => {
        // 如果这里 url 写的是网址, 就会跳转到对应网址 (二维码分享效果)
        QrCode.toCanvas(this.$refs.myCanvas, url)
      })
    },
    closeDialog() {
      this.isDialogShow = false
    },
    handleRoleAssign(id) {
      this.showRoleDialog = true
      this.userId = id
    }
  }
}
</script>
<style lang='scss' scoped>
.employees-container {
  .staff {
    // text-align: center;
    width: 70px;
    height: 70px;
    border-radius: 50%;
  }
}</style>
